package com.alice.controller;


import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import com.alice.util.RestResponse;
import com.alice.util.PageParam;
import com.alice.entity.User;
import com.alice.service.IUserService;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户表控制器
 * </p>
 *
 * @author ChenZhengHui
 * @since 2021-03-10
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户表服务接口")
public class UserController {


    @Autowired
    private IUserService userService;

    /**
    * 获取全部用户表列表
    */
    @ApiOperation("获取全部用户表列表")
    @GetMapping(value = "/all")
    public RestResponse<List<User>> list() {
        return RestResponse.ok(userService.list());
    }

    /**
     * 分页获取用户表列表
     */
    @ApiOperation("分页获取用户表列表")
    @GetMapping("/list")
    public RestResponse<IPage<User>> pageList(User user, @Valid PageParam pageParam){
        Wrapper wrapper = Wrappers.query(user);
        Page<User> pageObj = userService.page(new Page<>(pageParam.getPageNo(),pageParam.getSize()), wrapper);

        return RestResponse.ok(pageObj);
    }

    /**
     * 新增用户表
     */
    @ApiOperation("新增用户表")
    @PostMapping
    public RestResponse add(@RequestBody
            @Valid User user) {
        return RestResponse.ok(userService.save(user));
    }


    /**
     * 修改用户表
     */
    @ApiOperation(value="修改用户表")
    @PutMapping
    public RestResponse update(@RequestBody @Valid User user) {
        return RestResponse.ok(userService.updateById(user));
    }

    /**
     * 根据id删除用户表
     */
    @ApiOperation(value="根据id删除用户表")
    @DeleteMapping("/{ids}")
    public RestResponse delete(@PathVariable Long[] ids) {
        return RestResponse.ok(userService.removeByIds(Arrays.stream(ids).collect(Collectors.toSet())));
    }


    /**
     * 根据id获取用户表
     */
    @ApiOperation(value = "根据id获取用户表")
    @GetMapping(value = "/{id}")
    public RestResponse<User> get(@PathVariable  Long id) {
            return RestResponse.ok(userService.getById(id));
    }

}
